package com.whynot.frame.wechat.http.config;

import java.net.URL;
import java.util.Map;
import java.util.Properties;
import java.util.concurrent.ConcurrentHashMap;

import org.apache.commons.configuration.PropertiesConfiguration;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;


public class PropertiesUtils {

	private static String DEFAULT_PROPERTIES = "application.properties";

	private static Map<String,PropertiesConfiguration> configs= new ConcurrentHashMap<String,PropertiesConfiguration>();

	private static Logger log = LoggerFactory.getLogger(PropertiesUtils.class);

	public static String getProperty(String key, String... fileNames) {
		String value = null ;
		try {
			String propfile =  (fileNames.length > 0) ? fileNames[0] : DEFAULT_PROPERTIES;
			value =  getPropertiesConfiguration (propfile).getString(key);
		} catch (Exception e) {
			log.error("读取配置文件失败：" + e.getMessage());
		}
		return value ;
	}

	public static void setProperty(String key, String value,
			String... fileNames) {
		try {
			String propfile =  (fileNames.length > 0) ? fileNames[0] : DEFAULT_PROPERTIES;
			Properties props = locateFromClasspath(propfile);
			props.setProperty(key, value);
		} catch (Exception e) {
			log.error("写入配置文件失败：" + e.getMessage());
		}
	}

	static Properties locateFromClasspath(String resourceName) throws Exception {
		URL url = Thread.currentThread().getContextClassLoader().getSystemClassLoader().getResource(resourceName);
		Properties prop = new Properties();
		prop.load(url.openStream());
		return prop;
	}

	public static PropertiesConfiguration getPropertiesConfiguration(String resourceName) throws Exception {
		if(configs.containsKey(resourceName)) {
			return configs.get(resourceName);
		}else {
			PropertiesConfiguration config = new PropertiesConfiguration(resourceName);
			configs.put(resourceName, config);
			return config ;
		}
	}


}